package com.qf.controller;

import com.qf.entity.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author YDW
 * projectName:project2202_3
 * time:2022/5/15 17:37
 * description:
 */
@RestController
public class UserController {

    @PostMapping ("/doLogin")
    public Result login(String username, String password,Boolean rememberMe){
        if(username==null||username.length()==0){
            return Result.error(new RuntimeException("输入不能为空").getMessage());
        }

        //获取subject对象
        Subject subject = SecurityUtils.getSubject();

        //对密码进行加密
        //对密码进行md5加密，盐值为username，迭代次数为1024
        Md5Hash md5Hash=new Md5Hash(password,username,1024);

        //创建token对象
        UsernamePasswordToken token=new UsernamePasswordToken(username,md5Hash.toString(),rememberMe);

        //开始登录
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            System.out.println("未知用户异常");
            e.printStackTrace();
            return Result.error(e.getMessage());
        }catch (LockedAccountException e){
            System.out.println("账户已经锁定");
            e.printStackTrace();
            return Result.error(e.getMessage());
        }catch (CredentialsException e){
            System.out.println("密码有误");
            e.printStackTrace();
            return Result.error(e.getMessage());
        }


        System.out.println(subject.hasRole("管理员")? "是管理员":"不是管理员");
        System.out.println(subject.isPermitted("sys:user:add")? "有添加用户的权限":"没有添加用户的权限");
       return Result.ok("登录成功");
    }
    //退出
    @RequestMapping("/logout")
    public Result logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Result.ok();
    }

    /**
     * logical: Logical.OR 满足一个即可   Logical.AND 需要满足两个
     * @return
     */
    @RequiresRoles(value = {"管理员","开发工程师"},logical = Logical.OR)
    @RequestMapping("/add")
    public Result add(){
        System.out.println("添加");
        return Result.ok();
    }
    @RequiresRoles({"管理员"})
    @RequestMapping("/update")
    public Result update(){
        System.out.println("更新");
        return Result.ok();
    }

    @RequiresPermissions({"sys:user:del"})
    @RequestMapping("/del")
    public Result del(){
        System.out.println("删除");
        return Result.ok();
    }

    @RequiresPermissions({"sys:test:select"})
    @RequestMapping("/select")
    public Result select(){
        System.out.println("查询");
        return Result.ok();
    }
}
